package cs.xwi7xwa.activities;

import java.io.IOException;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import cs.xwi7xwa.R;
import cs.xwi7xwa.data.ExhibitDbAdapter;
import cs.xwi7xwa.data.FactDbAdapter;
import cs.xwi7xwa.templates.BumpTemplate;
import cs.xwi7xwa.templates.Xwi7xwaToast;

public class ExhibitView extends BumpTemplate {
    private String name;
    private String desc;
    private String imagePath;
    private int id = -1;

    private FactDbAdapter dbAdapter;
    private Cursor cursor;

    public static final String EXHIBIT_NAME = "exhibitName";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exhibit);

        // Get all extra data that came with the Intent
        Bundle extras = getIntent().getExtras();

        // Populate the page with the extra data
        if (extras != null) {
            name = extras.getString(ExhibitDbAdapter.KEY_NAME);
            desc = extras.getString(ExhibitDbAdapter.KEY_DESC);
            imagePath = extras.getString(ExhibitDbAdapter.KEY_IMAGE);
            // If the id does not exist, then use -1 as default
            id = extras.getInt(ExhibitDbAdapter.KEY_ID, -1);

            if (desc != null) {
                TextView descView = (TextView) findViewById(R.id.exhibitDesc);
                descView.setText(desc);
            }
            if (imagePath != null) {
                ImageView imageView = (ImageView) findViewById(R.id.exhibitImage);
                imageView.setImageResource(Integer.decode(imagePath));
            }
        }

        // Load the database
        dbAdapter = new FactDbAdapter(this);
        try {
            dbAdapter.open();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // TODO: Link buttons with each facts
        cursor = dbAdapter.fetchAll(id);
        startManagingCursor(cursor);

        // Log this method's execution
        log(null);
    }

    public void goToFacts(View view) {
        Cursor c = cursor;
        if (c.getCount() > 0) {
            c.moveToFirst();

            Intent intent = new Intent(view.getContext(), FactChartView.class);
            intent.putExtra(ExhibitDbAdapter.KEY_ID, id);
            intent.putExtra(ExhibitDbAdapter.KEY_NAME, name);

            startActivityForResult(intent, 0);

            // Log this method's execution
            log(new String[] { "ID: " + id, "Name: " + name });
        } else {
            // Display an error message
            View layout = getLayoutInflater().inflate(R.layout.toast,
                (ViewGroup) findViewById(R.id.toastText));
            Xwi7xwaToast noFact = new Xwi7xwaToast(layout,
                getApplicationContext(), "Sorry, no facts to display.");
            noFact.show();

            // Log this method's execution
            log(new String[] { "Error: No facts" });
        }
    }
}
